package com.guqj.basis.clone;

/**
 * <p>
 * 	浅复制(浅克隆)被复制对象的所有变量都含有与原来对象相同的值，而所有的对其他对象的引用仍然只指向原来的对象,
 * 	换言之，浅复制仅仅复制锁考虑的对象，而不复制它所引用的对象。(复制引用)
 * </p>
 * @author guqj
 * @date   2017年3月10日
 */
public class PeopleShallow implements Cloneable {

	private String name;
	private Integer age;
	private Address address; // 被克隆对象所共享
	
	public PeopleShallow(String name, int age, Address address){
		this.name = name;
		this.age = age;
		this.address = address;
	}
	
	@Override
	protected Object clone() throws CloneNotSupportedException {
		return (PeopleShallow)super.clone();
	}
	
	public static void main(String[] args) throws CloneNotSupportedException {
		Address address = new Address("中国", "上海", "上海");
		PeopleShallow p1 = new PeopleShallow("xiaoxiao", 20, address);
		PeopleShallow p2 = (PeopleShallow) p1.clone();
		p2.name = "lili";
		p2.age = 30;
		p2.address.setCity("闵行");
		System.out.println(p1 == p2);
		System.out.println("p1 age: " + p1.age);
		System.out.println("p1 address city: " + p1.address.getCity()); // p1.address.getCity()值会随着p2改变而改变
	}
}
